#include	<unistd.h>
#include	"my.h"

char		*epur_it(char *str)
{
  char		*dest;
  int		i;

  i = 0;
  dest = xmalloc(my_strlen(str) * sizeof(char));
  while (str[i] != ' ')
    {
      dest[i] = str[i];
      i++;
    }
  dest[i] = '\0';
  return (dest);
}

int		str_isoption(char *str)
{
  int		i;

  i = 0;
  while (str[i])
    {
      if (str[i] == ' ' && str[i+ 1] == '-')
	return (0);
      else
        i++;
    }
  return (1);
}

char		**exec_bin(char *str, char **tab, char **env)
{
  char		*bin;
  pid_t		pid;

  if ((pid = xfork()) == 1)
    return (NULL);
  if (pid == 0)
    {
      bin = xmalloc(my_strlen(str) * sizeof(str));
      if (str_isoption(str) == 1)
        {
          if (execve(str, tab, env) == -1)
	    cmd_error(tab);
        }
      else
        {
          bin = epur_it(str);
          if (execve(bin, tab, env) == -1)
	    cmd_error(tab);
        }
    }
  else
    xwait(&pid);
  return (env);
}

char		**my_bin(char *str, char **env, t_list *list)
{
  int		i;
  int		j;
  int		k;
  char		*tmp;

  i = 0;
  k = 0;
  tmp = xmalloc(my_strlen(str));
  while (str[i])
    {
      i++;
      j = i;
    }
  while (str[i] != '/')
    i--;
  i++;
  while (i != j)
    tmp[k++] = str[i++];
  tmp[k] = '\0';
  exec_bin(str, my_str_to_wordtab(tmp), env);
  value_prompt = 1;
  list->stop = 1;
  return (env);
}
